<template>
  <div class="body">
    <div class="top">
      <div class="back" @click="back"><i class="el-icon-back"></i></div>
      <div>全部MV</div>
    </div>
    <!-- tab 标签 -->
    <div class="tag">
      <div>地区:</div>
      <div v-for="item in areaData" :key="item.id" @click="changeArea(item)"
        :style="item.name === area ? 'color:#ec4141' : 'color:#000'">
        {{ item.name }}
      </div>
    </div>
    <div class="tag">
      <div>类型:</div>
      <div v-for="item in typeData" :key="item.id" @click="changeType(item)"
        :style="item.name === type ? 'color:#ec4141' : 'color:#000'">
        {{ item.name }}
      </div>
    </div>
    <div class="tag">
      <div>排序:</div>
      <div v-for="item in orderData" :key="item.id" @click="changeOrder(item)"
        :style="item.name === order ? 'color:#ec4141' : 'color:#000'">
        {{ item.name }}
      </div>
    </div>

    <!-- 内容展示 -->
    <div class="content" v-if="data !== null">
      <div v-for="item in data" :key="item.id" class="contentBox">
        <el-card shadow="always" :body-style="{ padding: '0px' }">
          <div class="count">
            <i class="el-icon-video-play"></i>{{
                item.playCount >= 10000
                  ? (item.playCount / 10000).toFixed(0) + "万"
                  : item.playCount
            }}
          </div>
          <el-image :src="item.cover" lazy fit="cover" @click="playMv(item)">
            <div slot="placeholder" class="image-slot">
              <el-image src="" lazy @click="playMv(item)"></el-image>
            </div>
          </el-image>
          <div style="padding: 14px">
            <el-tooltip class="item" effect="dark" :content="item.name" placement="top">
              <div class="desc">
                {{ item.name }}
              </div>
            </el-tooltip>
          </div>
        </el-card>
      </div>
    </div>
    <div class="more" @click="load()" v-if="more === true">
      查看更多 <i class="el-icon-arrow-down"></i>
    </div>
    <div class="loading" v-if="loading === true">
      <i class="el-icon-loading"></i>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';
import debounce from "@/utils/debounce.js";
export default {
  mounted() {
    const mv = window.sessionStorage.getItem('mv');
    if (mv === 'wy') {
      this.type = '网易出品';
    } else if (mv === 'hot') {
      this.order = '最热';
    }
    this.getData()
  },
  data() {
    return {
      area: '内地',
      type: '全部',
      order: '最新',
      areaData: [
        { id: 0, name: '内地' },
        { id: 1, name: '港台' },
        { id: 2, name: '欧美' },
        { id: 3, name: '日本' },
        { id: 4, name: '韩国' },
      ],
      typeData: [
        { id: 0, name: '全部' },
        { id: 1, name: '官方版' },
        { id: 2, name: '原声' },
        { id: 3, name: '现场版' },
        { id: 4, name: '网易出品' },
      ],
      orderData: [
        { id: 0, name: '上升最快' },
        { id: 1, name: '最热' },
        { id: 2, name: '最新' },
      ],
      limit: 30,
      offset: 0,
      more: false,
      data: null,
      count: 0,
      loading: false,
      number: 1,
    };
  },
  beforeRouteLeave(to, from, next) {
    this.loading = true
    next()
  },
  methods: {
    ...mapActions(['saveMVurl', 'saveResMv', 'saveMvBes', 'saveArtistsItem']),
    // 切换地区
    changeArea(e) {
      this.area = e.name;
      this.getData()
    },
    // 切换类型
    changeType(e) {
      this.type = e.name;
      this.getData()
    },
    // 切换排序
    changeOrder(e) {
      this.order = e.name;
      this.getData()
    },

    load() {
      if (this.more === true) {
        this.loading = true;
        this.number += 1;
        this.offset = this.number * 30;
        this.getData()
      }
    },

    // 获取数据
    getData: debounce(function () {
      this.$api
        .allMv({
          area: this.area,
          order: this.order,
          type: this.type,
          limit: this.limit,
          offset: this.offset,
          cookie: localStorage.getItem('cookie'),
        })
        .then((ret) => {
          this.more = ret.hasMore;
          this.count = ret.count;
          this.data = ret.data;
          this.loading = false;
        });
    }, 1000),

    back() {
      this.$router.go(-1);
    },

    // 播放MV
    async playMv(e) {
      // 获取到MV的地址
      this.$api.mvUrl({ id: parseInt(e.id), cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveMVurl({ mvUrl: ret.data.url });
      });
      this.getArtists(e.artistId);
      this.getResMv(e.id);
      this.getItem(e.id);
    },

    // 获取相似MV
    async getResMv(e) {
      this.$api.simiMv({ mvid: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveResMv({ resMv: ret });
      });
    },

    // 获取MV基础信息
    async getItem(e) {
      this.loading = true;
      this.$api.mvDetail({ mvid: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveMvBes({ mvBes: ret });
        this.$router.push(`/mvPlay?id=${e}`);
      });
    },

    // 获取MV作者
    async getArtists(e) {
      window.sessionStorage.setItem('artists', e);
      this.$api.arDetail({ id: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveArtistsItem({ artistsItem: ret.data });
      });
    },

    // 跳转到所有MV页面
    all(e) {
      window.sessionStorage.setItem('mv', e);
      this.$router.push('/allMv');
    },
  },
};
</script>

<style scoped>
.body {
  margin-bottom: 40px;
}

.tag {
  display: flex;
  margin-bottom: 10px;
  margin-bottom: 20px;
}

.tag div {
  margin-right: 20px;
  cursor: pointer;
  font-size: 20px;
}

.top {
  display: flex;
  align-items: center;
  font-size: 20px;
  margin-bottom: 20px;
}

.back {
  font-size: 30px;
}

.content {
  display: flex;
  flex-wrap: wrap;
}

.contentBox {
  margin-right: 5px;
  position: relative;
}

.count {
  position: absolute;
  z-index: 10;
  width: 100%;
  text-align: right;
  color: #fff;
}

.desc {
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.el-card {
  margin-bottom: 10px;
  cursor: pointer;
  width: 300px;
  position: relative;
}

.el-card .el-image {
  width: 300px;
  height: 200px;
}

.loading {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
}

.el-icon-loading {
  position: fixed;
  font-size: 50px;
  left: 50%;
  top: 50%;
  color: #ec4141;
}

.more {
  margin-top: 20px;
  cursor: pointer;
  text-align: center;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
